﻿var YCAutoCompleteFecha = 0;
var YCAutoCompleteCarrega = 0;
var ajax = null;

function YCAutoCompleteSeleciona(Key, txt, TxtId) {
    document.getElementById(TxtId).value = txt.replace("<b>", "").replace("</b>", "");
    SetValueHf(TxtId, Key);

    $('#' + TxtId).attr("txt", "");
    YCAutoCompleteRemoveAreaItens(TxtId);
}
function YCAutoCompleteRemoveAreaItens(id) {
    if (document.getElementById(id + "_txt") != null)
        document.getElementById(id).parentNode.removeChild(document.getElementById(id + "_txt"));
}
function SetValueHf(TxtId, value) {
    $('#' + $('#' + TxtId).attr("idHfSelectedValue")).attr("value", value);
}




function YCAutoComplete(th) {
    if (YCAutoCompleteCarrega != 0) clearTimeout(YCAutoCompleteCarrega);
    YCAutoCompleteCarrega = setTimeout(function () {
        if (th.value == "") {
            YCAutoCompleteRemoveAreaItens(th.id);
        } else if (th.value != th.getAttribute("txt")) {
            th.setAttribute("txt", th.value);
            SetValueHf(th.id, -1);
            var permite = false;
            var res = "";

            YCAutoCompleteRemoveAreaItens(th.id);

            var div = document.createElement("div");
            div.id = th.id + "_txt";
            div.style.position = "absolute";
            div.style.left = ($('#' + th.id).offset().left + 2) + "px";
            div.style.top = $('#' + th.id).offset().top + $('#' + th.id).height() + "px";
            div.style.width = $(th).width() + 'px';
            div.style.background = "#FFF";
            div.innerHTML = "Carregando <div class='YCAutoComplete_Loading' />";
            th.parentNode.appendChild(div);

            document.getElementById(th.id + "_txt").onmouseout = function () {
                YCAutoCompleteFecha = setTimeout(function () {
                    YCAutoCompleteRemoveAreaItens(th.id);
                }, 800);
            }

            document.getElementById(th.id + "_txt").onmouseover = function () {
                if (YCAutoCompleteFecha != 0)
                    clearTimeout(YCAutoCompleteFecha);
            }
            th.setAttribute("onblur", "");

            var txtret = "";
            if (ajax != null) ajax.abort();
            ajax = $.ajax({
                url: th.getAttribute("url") + encodeURIComponent(th.value),
                context: document.body,
                async: true,
                type: 'GET',
                success: function (data) {
                    res = eval('(' + data + ')');

                    if (res.length == 1) {
                        YCAutoCompleteSeleciona(decodeURIComponent(res[0].Key), decodeURIComponent(res[0].Value.replace("<b>", "").replace("</b>", "")), th.id);
                    } else {
                        for (i = 0; i < res.length; i++)
                            txtret += "<div onclick='YCAutoCompleteSeleciona(\"" + decodeURIComponent(res[i].Key) + "\", this.innerHTML, \"" + th.id + "\")' class='YCAutoComplete_itens'>" + decodeURIComponent(res[i].Value.replace(th.value + "", "<b>" + th.value + "</b>")) + "</div>";

                        if (txtret == "") {
                            th.style.background = "#EB4C42";
                            YCAutoCompleteRemoveAreaItens(th.id);

                            th.setAttribute("onblur", "this.value = '';SetValueHf('" + th.id + "', 0);this.style.background = '';");
                        } else {
                            th.style.background = "";
                            if (document.getElementById(th.id + "_txt") != null)
                                div.innerHTML = txtret;
                        }
                    }

                }
            });
        }
    }, 400);
}